package it.laypay.business;

import org.json.JSONObject;
import org.openqa.selenium.By;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Aruna Rajapaksha
 */

public class CodeManagementPage extends BusinessPage {

    //Elements
    String pageTitle = "- Laypay business portal"; //TODO

    By userName = By.id("UserName");
    By password = By.id("Password");
    By login = By.xpath("//input[@value='Log in']");

    By codeType = By.id("seProductType");
    By productName = By.id("ProductName");
    By externalReferenceID = By.id("ExternalReferenceID");
    Map<String, By> paymentType = new HashMap<String, By>();
    By paymentTypeDirectDebit = By.id("pay1");
    By paymentTypeVisaCard = By.id("pay2");
    By paymentTypeMastercard = By.id("pay3");
    By numberOfInstallment = By.id("NoOfInstallment");
    By instalmentType = By.id("seinstallmentType");
    By shortDescription = By.id("ShortDescription");
    By longDescription = By.id("LongDescription");
    By dueDate = By.id("DueDate");
    By canEditSchedule = By.id("CanEditSchedule");
    By canMakeTopUpPayment = By.id("CanMakeTopPayment");
    By totalCost = By.id("TotalCost");
    By productLogo = By.id("UploadImage");
    By canCancel = By.id("CanCancel");
    By coolingOffPeriod = By.id("CoolingOffPeriod");

    //Customer Data
    By title = By.id("CustomerTitle");
    By firstName = By.id("FirstName");
    By middleName = By.id("MiddleName");
    By lastName = By.id("LastName");
    By businessName = By.id("BusinessName");
    By email = By.id("Email");
    By mobileNo = By.id("MobileNo");
    By other = By.id("Other");
    By addressLine1 = By.id("AddressLine1");
    By addressLine2 = By.id("AddressLine2");
    By suburb = By.id("Suburb");
    By autoComplete = By.className("ui-corner-all");
    By state = By.id("sepoststate");
    By postalCode = By.id("PostPostCode");

    By create = By.cssSelector("input[type=\"submit\"]");

    String expectedLabelsCustomer = "Code Type,Product name,External reference ID,Payment type,Direct Debit," +
            "Visa Card,MasterCard,# of instalments,Instalment type,Short description,Long description,Due date," +
            "Can edit schedule,Can make top up payment,Total cost,Product logo,Can cancel,Cooling off period," +
            "Customer data,Title,First name,Middle name,Last name,Business name,E-mail,Mobile No,Other," +
            "Address line 1,Address line 2,Suburb,State,Postal code";
    String expectedLabelsProductPrice = "Code Type,Product name,External reference ID,Payment type," +
            "Direct Debit,Visa Card,MasterCard,# of instalments,Instalment type,Short description,Long description," +
            "Due date,Can edit schedule,Can make top up payment,Total cost,Product logo,Can cancel,Cooling off period";
    String expectedLabelsProductNoPrice = "Code Type,Product name,External reference ID,Payment type," +
            "Direct Debit,Visa Card,MasterCard,Short description,Long description,Can edit schedule," +
            "Can make top up payment,Product logo";

    public CodeManagementPage() {

        paymentType.put("Direct Debit", paymentTypeDirectDebit);
        paymentType.put("Visa Card", paymentTypeVisaCard);
        paymentType.put("MasterCard", paymentTypeMastercard);
    }

    public void login(JSONObject data) throws Exception {
        driver.get(BUSINESS_URL + "Account/Login");
        clearAndType(userName, data.getString("Email"));
        clearAndType(password, data.getString("Password"));
        click(login);
    }

    public void login() throws Exception {
        driver.get(BUSINESS_URL + "Account/Login");
        clearAndType(userName, BUSINESS_EMAIL);
        clearAndType(password, BUSINESS_PASSWORD);
        click(login);
    }

    public void logout() throws Exception {
        //driver.findElement(By.cssSelector("span.hello")).click();
        //driver.findElement(By.linkText("Logout")).click();
        driver.get(BUSINESS_URL + "Account/Login");
    }

    private void setPaymentTypes(String paymentTypes) throws Exception {
        String[] paymentType = paymentTypes.split(DATA_DELIMITER);
        for (String name : paymentType) {
            click(this.paymentType.get(name));
        }
    }

    public void validateLabelsCustomer() throws Exception {
        click(codeManagement);
        checkPageTitle(pageTitle);
        validateLabels(expectedLabelsCustomer);
    }

    public void validateLabelsProductPrice() throws Exception {
        click(codeManagement);
        selectDropDown(codeType, CODE_TYPE_PRODUCT_PRICE);
        while (isElementPresent(title)) {
            sleep(1);
        }
        checkPageTitle(pageTitle);
        validateLabels(expectedLabelsProductPrice);
    }

    public void validateLabelsProductNoPrice() throws Exception {

        click(codeManagement);
        selectDropDown(codeType, CODE_TYPE_PRODUCT_NO_PRICE);
        while (isElementPresent(title)) {
            sleep(1);
        }
        checkPageTitle(pageTitle);
        validateLabels(expectedLabelsProductNoPrice);
    }

    public void createCodeCustomer(JSONObject data) throws Exception {

        click(codeManagement);

        //Code Type
        clearAndType(productName, data.getString("ProductName"));
        clearAndType(externalReferenceID, data.getString("ExternalReferenceID"));
        setPaymentTypes(data.getString("PaymentTypes"));
        clearAndType(numberOfInstallment, data.getString("NumberOfInstallment"));
        selectDropDown(instalmentType, data.getString("InstalmentType"));
        clearAndType(shortDescription, data.getString("ShortDescription"));
        clearAndType(longDescription, data.getString("LongDescription"));
        clearAndType(dueDate, data.getString("DueDate"));
        click(canEditSchedule, data.getString("CanEditSchedule"));
        click(canMakeTopUpPayment, data.getString("CanMakeTopUpPayment"));
        clearAndType(totalCost, data.getString("TotalCost"));
        setFilePath(productLogo, data.getString("ProductLogo"));
        click(canCancel, data.getString("CanCancel"));
        clearAndType(coolingOffPeriod, data.getString("CoolingOffPeriod"));

        //Customer
        selectDropDown(title, data.getString("CustomerTitle"));
        clearAndType(firstName, data.getString("FirstName"));
        clearAndType(middleName, data.getString("MiddleName"));
        clearAndType(lastName, data.getString("LastName"));
        clearAndType(businessName, data.getString("BusinessName"));
        clearAndType(email, data.getString("Email"));
        clearAndType(mobileNo, data.getString("MobileNo"));
        clearAndType(other, data.getString("Other"));
        clearAndType(addressLine1, data.getString("AddressLine1"));
        clearAndType(addressLine2, data.getString("AddressLine2"));
        selectAutoComplete(suburb, autoComplete, data.getString("Suburb"));
        readAndCompare(state, data.getString("sepoststate"));
        readAndCompare(postalCode, data.getString("PostPostCode"));

        click(create);

    }

    public void createCodeProductPrice(JSONObject data) throws Exception {

        click(codeManagement);
        selectDropDown(codeType, CODE_TYPE_PRODUCT_PRICE);
        while (isElementPresent(title)) {
            sleep(1);
        }

        clearAndType(productName, data.getString("ProductName"));
        clearAndType(externalReferenceID, data.getString("ExternalReferenceID"));
        setPaymentTypes(data.getString("PaymentTypes"));
        clearAndType(numberOfInstallment, data.getString("NumberOfInstallment"));
        selectDropDown(instalmentType, data.getString("InstalmentType"));
        clearAndType(totalCost, data.getString("TotalCost"));
        clearAndType(dueDate, data.getString("DueDate"));
        clearAndType(shortDescription, data.getString("ShortDescription"));
        clearAndType(longDescription, data.getString("LongDescription"));
        click(canEditSchedule, data.getString("CanEditSchedule"));
        click(canMakeTopUpPayment, data.getString("CanMakeTopUpPayment"));
        setFilePath(productLogo, data.getString("ProductLogo"));
        click(canCancel, data.getString("CanCancel"));
        clearAndType(coolingOffPeriod, data.getString("CoolingOffPeriod"));

        click(create);
    }

    public void createCodeProductNoPrice(JSONObject data) throws Exception {

        click(codeManagement);
        selectDropDown(codeType, CODE_TYPE_PRODUCT_NO_PRICE);
        while (isElementPresent(title)) {
            sleep(1);
        }

        clearAndType(productName, data.getString("ProductName"));
        clearAndType(externalReferenceID, data.getString("ExternalReferenceID"));
        setPaymentTypes(data.getString("PaymentTypes"));
        clearAndType(shortDescription, data.getString("ShortDescription"));
        clearAndType(longDescription, data.getString("LongDescription"));
        click(canEditSchedule, data.getString("CanEditSchedule"));
        click(canMakeTopUpPayment, data.getString("CanMakeTopUpPayment"));
        setFilePath(productLogo, data.getString("ProductLogo"));

        click(create);
    }
}
